package com.singlemountaintech.haogongjiang.qfd.ui.demo;

import android.content.Intent;
import android.os.Bundle;

import com.singlemountaintech.haogongjiang.R;
import com.singlemountaintech.haogongjiang.event.RxBus;
import com.singlemountaintech.haogongjiang.event.RxBus_GoLogin;
import com.singlemountaintech.haogongjiang.qfd.BaseActivityNoSlidePresenter;
import com.singlemountaintech.haogongjiang.qfd.ITabFragment;
import com.singlemountaintech.haogongjiang.qfd.constant.Keys;
import com.singlemountaintech.haogongjiang.qfd.ui.invest.fragment.TabInvestFragment;
import com.singlemountaintech.haogongjiang.qfd.ui.main.fragment.TabMainFragment;
import com.singlemountaintech.haogongjiang.utils.CodeUtils;
import com.singlemountaintech.haogongjiang.utils.LogUtils;
import com.singlemountaintech.haogongjiang.widgets.tab.TabLayout;

import java.util.ArrayList;

import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by LUOLI on 2017/3/7.
 */
public class HomeActivity extends BaseActivityNoSlidePresenter<HomeDelegate> implements TabLayout.OnTabClickListener {
    private static final String TAG = HomeActivity.class.getSimpleName();
    private ITabFragment mTabFragment;
    private Subscription subscribe;

    @Override
    protected Class<HomeDelegate> getDelegateClass() {
        return HomeDelegate.class;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ArrayList<TabLayout.Tab> tabs = new ArrayList<>();
        tabs.add(new TabLayout.Tab(R.drawable.selector_tab_contact, R.string.home_tab_main, TabMainFragment.class));
        tabs.add(new TabLayout.Tab(R.drawable.selector_tab_moments, R.string.home_tab_invest, TabInvestFragment.class));
        viewDelegate.setTabs(tabs, this);
    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        subscribe = RxBus.getDefault().toObservable(RxBus_GoLogin.class)
                .subscribe(new Action1<RxBus_GoLogin>() {
                    @Override
                    public void call(RxBus_GoLogin rxBus_goLogin) {
                        LogUtils.eNormal(TAG,"哈哈哈");
                        Intent intent = new Intent(HomeActivity.this, LoginActivity.class);
                        intent.putExtra(Keys.VALUE,"refresh");
                        startActivity(intent);
                        CodeUtils.starActivityDown(HomeActivity.this);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (!subscribe.isUnsubscribed()) {
            subscribe.unsubscribe();
        }
    }

    @Override
    protected void refresh() {
        super.refresh();
    }

    @Override
    public void onTabClick(TabLayout.Tab tab) {
        try {
            ITabFragment tmpFragment = (ITabFragment) getSupportFragmentManager().findFragmentByTag(tab.targetFragmentClz.getSimpleName());
            if (tmpFragment == null) {
                tmpFragment = tab.targetFragmentClz.newInstance();
                if (mTabFragment == null) {
                    getSupportFragmentManager().beginTransaction()
                            .add(R.id.mFragmentContainerLayout, tmpFragment.getFragment(), tab.targetFragmentClz.getSimpleName())
                            .commit();
                } else {
                    getSupportFragmentManager().beginTransaction()
                            .hide(mTabFragment.getFragment())
                            .add(R.id.mFragmentContainerLayout, tmpFragment.getFragment(), tab.targetFragmentClz.getSimpleName())
                            .commit();
                }
            } else {
                if (mTabFragment == null) {
                    getSupportFragmentManager().beginTransaction()
                            .show(tmpFragment.getFragment())
                            .commit();
                } else {
                    getSupportFragmentManager().beginTransaction()
                            .hide(mTabFragment.getFragment())
                            .show(tmpFragment.getFragment())
                            .commit();
                }
            }
            mTabFragment = tmpFragment;
//            viewDelegate.setTitle(mTabFragment.getTitle());

        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }


    public void hiddenToolBar() {
//         viewDelegate.hiddenToolBar();
    }
}
